<?php
namespace Home\Controller;
use Think\Controller;
class DownController extends CommonController {

    // public function _initialize(){
    //     //判断用户是否已经登录
    //     if (!isset($_SESSION['uid'])) {
    //         $this->error('对不起,您还没有登录!请先登录再进行浏览', U('Login/login'), 1);
    //     }
    // }


    //太阳高效搜索， 搜索控制器,搜索后跳转到搜索主页
    public function index()
    {
     
       $cate =D('cate'); //读取分类总表
       $users =D('users');
       $id=(int) session('uid');
       //dump($id);
       $usersvip=$users->field('userid,vip')->where(array('userid'=>$id))->select();//用户级别查找
       
       //dump($cateaddoid);
       //dump($usersvip);
       $this->assign('usersvip',$usersvip);// 赋值超链数据集

       //dump($usersvip);
         $cates=$cate->where('id=329')->find();
      



 
       //dump($cates); //null

       // $catesons=$cate->where('pid='.$cates['id'])->select(); //获取子分类和其id



       $where=array('pid'=>0); //只显示顶级分类
       $navres=$cate->field('id,name,type')->order('sort asc')->where($where)->select();
       //dump($navres);
      
       $this->assign('navres',$navres);        //顶部 顶级分类


            if($cates['pid']==0 && !$catesons){
               $cates=$cate->where('id=329')->find();
              
               

              }
              //dump($cates);//以获取二级分类名称
              //dump($where); //为null  空
              
              // $catesons=$cate->field('id,name')->where('pid='.$cates['id'])->select();//只取id 和name字段


 
               //dump($catesons);
              if($kw=I('kws')){ // 2.获取关键字$kw   kw不为空
                  

               }
                //dump($catesons);
                //dump($kw);
              
              

            $ret = array();  //用[]系统报错 5.3版识别不出来
            foreach ($catesons as $k=>$v) {
                if($v['name']==$kw)
                {
                    $ret['']=$v['id'];                
                }
            }
                  // return $ret;    
       //dump($ret);
                
         
       //获取二级分类下的超链        
        $ret['cateid']= array('eq',$ret['']); //用$ret['']
        //dump($ret['cateid']);
        //$cate=D('cate'); //1.先实例化cate表，获得数据
        //dump($cate);
        $slink=D('slink'); //1.先实例化slink表，获得数据

        $count=$slink->where($ret)->count();// 查询满足要求的总记录数
    
        $page=new \Think\Page($count,24);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $listas = $slink->where($ret)->limit($page->firstRow.','.$page->listRows)->select();
        //dump($listas);   //cateid=$cateid
        $show= $page->show();// 分页显示输出
        //dump($show);
        //
        //
        //    //Tags标签数组
        $tagsArr=$this->getTags();  
     
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('listas',$listas);// 赋值超链数据集

        $this->assign('catesons',$catesons);

        $cateaddoid=$cate->field('id')->where('pid=0')->select();
        $this->assign('cateaddoid',$cateaddoid);// 赋值超链数据集
    //dump($usersvip);
        $this->assign('tagsArr',$tagsArr); //标签
        $this->assign('ret',$ret); //标签
        $this->assign('kw',$kw);  //把值赋给前台模板

        $this->display();
       
    } 


    public function add(){

   if(IS_POST){
            //放在post里
            $slink=D('slink'); //1.先实例化cate表，获得数据,不可少

            $data['title']    = I('title');
            $data['link']     = I('link');
            $data['cateid']   = I('cateidurl');
          

            $upload = new \Think\Upload();// 先实例化上传类 
            $upload->maxSize   = 3145728 ;// 设置附件上传大小 
            $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  = './slink/'; // 设置附件上传目录  
            $upload->rootPath  = './Upload/';
            // 上传文件     
            $info   =   $upload->upload(); 
        
            $data['tags']     =I('tags');
            $data['tags2']    =I('tags2');
            $data['tags3']    =I('tags3');
            $data['tags4']    =I('tags4');
            $data['tags5']    =I('tags5');
            $data['tags6']    =I('tags6');
            $data['tags7']    =I('tags7');
            $data['tags8']    =I('tags8');
            $data['tags9']    =I('tags9');
            $data['tags10']   =I('tags10');
            $data['author']   =$_SESSION['username']; 

            $lu =date("Y-m-d");
            $data['img']="$lu/".$info['img']['savename'];

            $data['content'] =I('content');
            //$data['verify']  =I('verify');

            
            $data['time']=time();
            $data['create_time'] = NOW_TIME;

       
            
             
      if($slink->create($data)){
     // dump($data);
            if($slink->add()){
                 $this->success('添加成功！');
            

            }else{
                $this->error('提交失败！');
            }
        }else{
            $this->error($slink->getError());
        }
        return;
        } 
        $this->display();

 } 

  //添加搜索词

  public function addo(){

        //var_dump(I('path.2'));
 


  if(IS_POST){
            //放在post里
            $cate=D('cate'); //1.先实例化cate表，获得数据,不可少

            $data['name']    = I('name');
             $data['pid']    = I('path.2');

             //$slink=D('slink');
       
            
             
      if($cate->create($data)){
      //dump($data);
            if($cate->add()){
                $this->success('添加成功！');

            }else{
                $this->error('提交失败！');
            }
        }else{
            $this->error($cate->getError());
        }
        return;
        } 
        $this->display();

 } 

    //添加搜索栏

  public function addc(){
    
    if(IS_POST){

      // $upload = new \Think\Upload();// 先实例化上传类 
      // $upload->maxSize   = 3145728 ;// 设置附件上传大小 
      // $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
      // $upload->savePath  = './wblogo/'; // 设置附件上传目录  
      // $upload->rootPath  = './Upload/';
      // // 上传文件     
      // $info   =   $upload->upload();  

      // echo"<pre>";
      // print_r($info);

      // if(!$info) {
      // // 上传错误提示错误信息        
      // $this->error($upload->getError());
      // }else{
      // // 上传成功        
      // $this->success('上传成功！');  
      }

      
        //dump($_POST); 

        
      //左边是数据库，右边是传过来的
      $data =array();
     
      //注意二位数组读取方式
      $lu =date("Y-m-d");
      

      $data['logo']="$lu/".$info['logo']['savename'];
   
      $data['name']=I("name","",''); //公司名称
      $data['link']=I("link");

      //$des  = html_entity_decode($_POST[codeDes]);//html_entity_decode   htmlspecialchars


      $data['des']=I("des");  //招聘内容
    
  
      
      $data['create_time'] = NOW_TIME;

     
      M("Web")->where("id ='{$id}'")->save($data);
      // dump($data);
    
     // header("Location:".U("search/index"));



    $this->display();

  }

  //添加搜索栏

  public function addi(){
    
    if(IS_POST){

      // $upload = new \Think\Upload();// 先实例化上传类 
      // $upload->maxSize   = 3145728 ;// 设置附件上传大小 
      // $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
      // $upload->savePath  = './wblogo/'; // 设置附件上传目录  
      // $upload->rootPath  = './Upload/';
      // // 上传文件     
      // $info   =   $upload->upload();  

      // echo"<pre>";
      // print_r($info);

      // if(!$info) {
      // // 上传错误提示错误信息        
      // $this->error($upload->getError());
      // }else{
      // // 上传成功        
      // $this->success('上传成功！');  
      }

      
        //dump($_POST); 

        
      //左边是数据库，右边是传过来的
      $data =array();
     
      //注意二位数组读取方式
      $lu =date("Y-m-d");
      

      $data['logo']="$lu/".$info['logo']['savename'];
   
      $data['name']=I("name","",''); //公司名称
      $data['link']=I("link");

      //$des  = html_entity_decode($_POST[codeDes]);//html_entity_decode   htmlspecialchars


      $data['des']=I("des");  //招聘内容
    
  
      
      $data['create_time'] = NOW_TIME;

     
      M("Web")->where("id ='{$id}'")->save($data);
      // dump($data);
    
     // header("Location:".U("search/index"));



    $this->display();

  }
    public function getTags(){
        $tags=C('tags');
        $tagsArr=explode(',', $tags); //explode 数组打散为字符串
        return $tagsArr;
    }







    //编辑搜索栏
  public function edite(){

  }
    //更新搜索栏
  public function updata(){

  }

    //删除搜索栏
  public function del(){

  }


    //用户退出
    public function loginOut(){
       
        //卸载session
        session_unset();
        //销毁session
        session_destroy();

        //删除用于自动登录的cookie
        @setcookie("auto",'',time() - 3600,'/');
        $this->redirect('Index/index');

    }
  





}